package com.shenma2009.message;

import com.rabbitmq.client.Channel;
import com.shenma2009.service.TbOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 军哥
 * @version 1.0
 * @description: OrderMessageRecv
 * @date 2023/4/27 14:41
 */
@Component
@Slf4j
public class OrderMessageRecv {

    @Autowired
    TbOrderService tbOrderService;

    @RabbitListener(queues = "orderTimeOutQueue")
    public void OnOrderMessage(Message message, Channel channel) throws IOException {
        // 获取消息ID
        String messageId = message.getMessageProperties().getMessageId();
        log.warn("messageId:"+messageId);

        // 获取消息内容
        String body = new String(message.getBody());
        log.warn("body:"+body);

        Integer orderId = Integer.valueOf(body);

        // 检查并取消订单
        tbOrderService.cancelOrder(orderId);

        // 手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
